<template>
  <div class="container">
    <div class="header">我的订单</div>
    <van-tabs v-model:active="state" @click-tab="onClickTab">
      <van-tab title="全部" name=""></van-tab>
      <van-tab title="待支付" name="1"></van-tab>
      <van-tab title="待服务" name="2"></van-tab>
      <van-tab title="已完成" name="3"></van-tab>
      <van-tab title="已取消" name="4"></van-tab>
    </van-tabs>
    <van-row @click="gotoDetail(item.out_trade_no)"
             class="van-row"
             v-for="(item,index) in listData"
             :key="item.out_trade_no">
      <van-col span="5">
        <van-image :src="item.serviceImg" width="50" height="50" radius="5"/>
      </van-col>
      <van-col span="14">
        <div class="text1">{{ item.service_name }}</div>
        <div class="text2">
          <div>{{ item.hospital_name }}</div>
          <div>预约时间：{{ item.starttime }}</div>
        </div>
      </van-col>
      <van-col span="5" class="text2" :style="{color : colouMap[item.trade_state]}">
        {{ item.trade_state }}
        <Counter :second="item.timer" v-if="item.trade_state === '待支付' && item.timer >0"/>
      </van-col>
    </van-row>
    <div class="bottom-text">没有更多了</div>
  </div>
</template>
<script setup>
import {getCurrentInstance, onMounted, ref} from 'vue'
import {useRouter, useRoute} from 'vue-router'

const router = useRouter()
const route = useRoute()
import Counter from '../../components/counter.vue'

const colouMap = {
  '待支付': '#ffa200',
  '待服务': '#1da6fa',
  '已完成': '#07c160',
  '已取消': '#ee0a24'
}
// 获取当前实例
const {proxy} = getCurrentInstance()

const state = ref('')
const listData = ref([])
//总的支付时间2小时
const payTime = ref(7200000)
onMounted(() => {
  if (route.query.state) {
    state.value = route.query.state
  }
  getOrderList(state.value)
})
const getOrderList = async (state) => {
  // 获取订单列表页面数据
  const {data} = await proxy.$api.orderList(state)
  if (data.code === 10000) {
    data.data.forEach(item => {
      item.timer = item.order_start_time + payTime.value - new Date().getTime()
    })
    listData.value = data.data
    console.log('listData', listData)
  }
}
const onClickTab = (item) => {
  getOrderList(item.name)
}

const gotoDetail = (oid) => {
  router.push('/detail?oid=' + oid)
}

</script>
<style lang="less" scoped>
.container {
  background-color: #f0f0f0;
  height: 100vh;
}

.header {
  background-color: #fff;
  line-height: 40px;
  text-align: center;
}

.van-row {
  background-color: #fff;
  padding: 10px;
  margin: 5px;
  border-radius: 5px;

  .text1 {
    font-size: 16px;
    line-height: 25px;
    font-weight: bold;
  }

  .text2 {
    font-size: 14px;
    line-height: 20px;
    color: #999999;
  }
}

.bottom-text {
  line-height: 50px;
  text-align: center;
  color: #999999;
}
</style>
